package multiProocess;

import lombok.Data;

@Data
class MySyncThread implements Runnable {
    private Integer ticket;
    private Integer threadCount;
    private String name;

    public MySyncThread(Integer ticket, Integer threadCount, String name) {
        this.ticket = ticket;
        this.threadCount = threadCount;
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < threadCount; i++) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()
                            + name + "- 卖票：ticket = " + ticket--);
                }
            }
        }
    }
}
public class SyncDemo2 {
    public static void main(String[] args) {
        MySyncThread mt = new MySyncThread(100, 100, "买票");
        Thread t1 = new Thread(mt, "A");
        Thread t2 = new Thread(mt, "B");
        Thread t3 = new Thread(mt, "C");
        t1.setPriority(10);
        t2.setPriority(1);
        t3.setPriority(1);
        t1.start();
        t2.start();
        t3.start();
    }
}
